package com.yunshipei.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseIntentBuilder;
import com.yunshipei.common.Globals;
import com.yunshipei.enterplorer.R;
import com.yunshipei.enterplorer.browser.HomeSearchWebViewClient;
import com.yunshipei.enterplorer.browser.HomeWebChromeClient;
import com.yunshipei.enterplorer.browser.HomeWebView;
import com.yunshipei.inter.JsInterface.HomeSearchJsObject;

public class HomeSearchActivity extends BaseActivity {

    private static final String EXTRA_HOME_URL = "com.enterplorer.extra.home_search_url";
    public static final String HOME_START_URL = "home_start_url";

    private HomeWebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_search);
        String url = getIntent().getStringExtra(EXTRA_HOME_URL);
        mWebView = (HomeWebView) findViewById(R.id.wv_home_search);
        mWebView.addJavascriptInterface(new HomeSearchJsObject(this), Globals.JS_OBJECT_ENTERPLORER_HOME_SEARCH);
        mWebView.setWebViewClient(new HomeSearchWebViewClient(this, url, mWebView));
        mWebView.setWebChromeClient(new HomeWebChromeClient(mWebView));
        mWebView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.stopLoading();
            mWebView.onDestroy();
            mWebView = null;
        }
    }

    public static class HomeSearchIntentBuilder extends BaseIntentBuilder {

        public HomeSearchIntentBuilder(Context context) {
            super(context);
        }

        public HomeSearchIntentBuilder setURL(String url) {
            getIntent().putExtra(EXTRA_HOME_URL, url);
            return this;
        }

        @Override
        protected Class<?> getTargetClazz() {
            return HomeSearchActivity.class;
        }
    }
}
